package job.manager.client.mb;

import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.model.SelectItem;
import job.manager.db.ec.AddressEB;
import job.manager.db.ec.ProfilesEB;
import job.manager.db.sb.ProfileRemote;

/**
 *  The profile ManagedBean is responsible for connection to the Enterprise Beans
 *  and providing the pages/user with the information about profiles.
 *  Also responsible for creating profiles.
 */
public class ProfileMB implements ActionListener {

    @EJB
    private ProfileRemote profileBean;
    private ProfilesEB profile;
    private boolean isEmployee;
    private String fname;
    private String lname;
    private String address1;
    private String address2;
    private String postcode;
    private String city;
    private String country;
    private String phone1;
    private String phone2;
    private String email;
    private String comment;
    private String number;

    /** Creates a new instance of ProfileMB */
    public ProfileMB() {
        this.onInit();
    }

    @PostConstruct
    public void onInit() {
    }

    //get all the profiles
    public ArrayList<SelectItem> getAllProfileList() {
        problemSolver();
        ArrayList<SelectItem> itemList = new ArrayList<SelectItem>();
        ArrayList<ProfilesEB> profs = new ArrayList(this.profileBean.getAllProfiles());

        for (ProfilesEB p : profs) {
            SelectItem a = new SelectItem();
            a.setLabel(p.getName1() + " " + p.getName2());
            a.setValue(p.getId().toString());
            itemList.add(a);
        }
        return itemList;
    }

    //action for creating a new profile
    public void processAction(ActionEvent event) throws AbortProcessingException {
        this.problemSolver();
        this.profile = new ProfilesEB(null, fname, lname, number, comment, isEmployee, !isEmployee);
        profile = profileBean.insertProfile(profile);
        if (profile != null) {
            AddressEB a = new AddressEB(null, address1, address2, postcode, city, country, phone1, phone2, email, "" + fname + "s Address", profile);
            profileBean.insertAddress(a);
        }
    }

    //bug fixer with spring security
    private void problemSolver() {
        if (this.profileBean == null) {
            this.profileBean = ConnectFactory.getProfileBean();
        }
    }

    //set and get methods
    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public boolean isIsEmployee() {
        return isEmployee;
    }

    public void setIsEmployee(boolean isEmployee) {
        this.isEmployee = isEmployee;
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getPhone1() {
        return phone1;
    }

    public void setPhone1(String phone1) {
        this.phone1 = phone1;
    }

    public String getPhone2() {
        return phone2;
    }

    public void setPhone2(String phone2) {
        this.phone2 = phone2;
    }

    public String getPostcode() {
        return postcode;
    }

    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }
}
